import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:vape_xe/utils/utils.dart';

class TeamController extends GetxController {
  /// 响应式成员变量
  final teamList = [].obs;

  /// 成员变量
  int pageSize = 20;
  int pageNumber = 1;
  final RefreshController refreshController = RefreshController(
    initialRefresh: false,
  );

  /// 事件
  void onRefresh() async {
    fetchNewsList(isRefresh: true).then((_) {
      refreshController.refreshCompleted(resetFooterState: true);
    }).catchError((e) {
      refreshController.refreshFailed();
    });
  }

  void onLoading() async {
    fetchNewsList().then((_) {
      refreshController.loadComplete();
    }).catchError((e) {
      Get.log('获取数据失败：$e');
      refreshController.loadFailed();
    });
  }

  Future<void> fetchNewsList({bool isRefresh = false}) async {
    var result = await Request().get('/user/teamList', {
      'pageSize': pageSize,
      'pageNumber': pageNumber,
    });

    if (isRefresh == true) {
      pageNumber = 1;
      teamList.clear();
    } else {
      pageNumber++;
    }
    teamList.addAll(result['result']);
  }

  /// 生命周期
  @override
  void onReady() async {
    super.onReady();
    fetchNewsList(isRefresh: true);
  }
}
